using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MyApi.Api.Entity;
using MyApi.Api.ParamModel;
using MyApi.Api.Repository;
using MyApi.Api.Utils;


namespace MyApi.Api.Controllers
{
    //[Authorize]
    [ApiController]
    [Route("[controller]")]
    public class UsersController : ControllerBase
    {
        // private MyApi.Api.Db.MyApiDb db = new Api.Db.MyApiDb();
         private IRepository<Users> _usersRespository;
         private TokenParameter _tokenParameter;
         private readonly IConfiguration _configuration;

         public UsersController(IConfiguration configuration,IRepository<Users> UsersRespository){
             _usersRespository=UsersRespository;
             _configuration=configuration;
             _tokenParameter=_configuration.GetSection("TokenParameter").Get<TokenParameter>();
         }
        [HttpGet]
        public IEnumerable<dynamic> Get()
        {
            var user = _usersRespository.Table.ToList();
            return user;
        }

        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var user = _usersRespository.GetById(id);
            if(user==null){
                return new{
                    Code=1001,
                    Data ="",
                    Msg ="用户不存在",
                };
            }
            return new{
                    Code=1000,
                    Data =user,
                    Msg ="用户以获取",
            };
        }

        [HttpPost]
        public dynamic post(NewUser model)
        {
            var user=new Users{
                Username=model.Username,
                Password=model.Password,
            };
            _usersRespository.Insert(user);
          

           return new
           {
              Code=1000,
              Data =user,
              Msg="创建成功"
           };
        }

         [HttpPut("{id}")]
        public dynamic Put(int id,NewUser model)
        {
           var user = _usersRespository.GetById(id);

            if (user != null)
            {
                user.Username = model.Username;
                user.Password = model.Password;
                user.UpdatedTime=DateTime.Now;
                _usersRespository.Update(user);
                return new
                {
                    Code = 1000,
                    Data = user,
                    Msg = string.Format("你修改的用户的id为：{0}，已经修改成功，请注意查收", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                };
            }
        

        }

         [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
        //   var user = db.Users.Where(x => x.Id == id).FirstOrDefault();
        var user=_usersRespository.GetById(id);
            if (user != null)
            {
                // db.Users.Remove(user);
                // db.SaveChanges();
                _usersRespository.Delete(id);
                return new
                {
                    Code = 1000,
                    Msg = string.Format("删除指定id为{0}的用户成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "指定Id的用户不存在，请确认后重试"
                };
            }
       }

        [HttpPost, Route("token")]
        public dynamic GetToken(NewUser model)
        {
            var username = model.Username.Trim();
            var password = model.Password.Trim();

            var user = _usersRespository.Table.Where(x => x.Username == username && x.Password == password).FirstOrDefault();

            if (user == null)
            {
                return new
                {
                    Code = 1001,
                    Data = user,
                    Msg = "用户名或密码不正确，请核对后重试"
                };
            }

            var token = TokenHelper.GenerateToekn(_tokenParameter, user);
            var refreshToken = "123456";

            var res = new
            {
                Code = 1000,
                Data = new { Token = token, RefreshToken = refreshToken },
                Msg = "用户登录成功，获取token成功"
            };
            return res;
        }

    }
}
